package com.wss.lsl.lucene.demo.searcher;

import java.io.IOException;

import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.Term;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class SearcherTest {

	private SearcherUtil su = null;

	@Before
	public void setUp() throws Exception {
		su = new SearcherUtil();
	}

	@After
	public void teardown() {
		try {
			su.closeIndex();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Test
	public void searchByTerm() throws CorruptIndexException, IOException {
		System.out.println("-------------------------------------");
		su.searchByTerm("sender", "zhangsan", 5);
		System.out.println("-------------------------------------");
		su.searchByTerm("content", "Apache", 6);
		System.out.println("-------------------------------------");
	}

	@Test
	public void searchByTermRange() {
		System.out.println("-------------------------------------");
		su.searchByRangeTerm("id", "1", "2", true, true, 4);
		System.out.println("-------------------------------------");
		su.searchByRangeTerm("sender", "a", "s", true, true, 4);
		System.out.println("-------------------------------------");
		// 查询不出数字field
		su.searchByRangeTerm("attach", "1", "30", true, true, 10);
		System.out.println("-------------------------------------");
	}

	@Test
	public void searchByNumericTerm() {
		System.out.println("-------------------------------------");
		su.searchByNumericTerm("attach", 1, 30, true, true, 10);
		System.out.println("-------------------------------------");
	}

	@Test
	public void searchByPrefixQuery() {
		System.out.println("-------------------------------------");
		su.searchByPrefixQuery("content", "i", 2);
		System.out.println("-------------------------------------");
		su.searchByPrefixQuery("email", "aa", 2);
		System.out.println("-------------------------------------");
		su.searchByPrefixQuery("sender", "w", 2);
		System.out.println("-------------------------------------");
	}

	@Test
	public void searchByWildcardQuery() {
		System.out.println("-------------------------------------");
		su.searchByWildcardQuery("content", "i*", 2);
		System.out.println("-------------------------------------");
		su.searchByWildcardQuery("sender", "w??", 2);
		System.out.println("-------------------------------------");
		su.searchByWildcardQuery("sender", "w*", 2);
		System.out.println("-------------------------------------");
	}

	@Test
	public void searchByBooleanQuery() {
		System.out.println("-------------------------------------");
		su.searchByBooleanQuery(new Term("sender", "wss"), new Term("content",
				"apache"), 4);
		System.out.println("-------------------------------------");
		su.searchByBooleanQuery(new Term("sender", "wss"), new Term("content",
				"walks"), 4);
		System.out.println("-------------------------------------");
	}
	
	@Test
	public void searchByPhraseQuery(){
		System.out.println("-------------------------------------");
		su.searchByBooleanQuery(new Term("content", "start"), new Term("content",
				"what"), 4);
		System.out.println("-------------------------------------");
		su.searchByBooleanQuery(new Term("content", "written"), new Term("content",
				"log"), 4);
		System.out.println("-------------------------------------");
	}
	
	@Test
	public void searchByFuzzyQuery(){
		System.out.println("-------------------------------------");
		su.searchByFuzzyQuery("sender", "wsa", 10);
		System.out.println("-------------------------------------");
	}

}
